# Top-level Makefile for Project 1.
# 
# Targets:
#   gmake [ default ]        Compiles sources in src directory
#   gmake check              Compiles sources and runs tests.  The parameter
#                            APYC indicates which compiler to run.
#   gmake clean              Removes unnecessary files, including those that
#                            are reproducible.
#

APYC = ./apyc

LEX = flex

# C++ compilation flags: produce debugging data, warn about numerous errors,
# and treat warnings as errors.
CXXFLAGS = -g -Wall -Werror

GOODTEST = python tests/test-proj --stage=2

BADTEST = python tests/test-proj --stage=2 --errors

# List of all C++ object files
OBJS = apyc.o parser.o ast.o ast-parser.o nodes.o decls.o types.o semantic.o environment.o

# List of all C++ sources generated by tools
GENERATED_SRCS = parser.cc lexer.cc ast-parser.cc ast-lexer.cc

.PHONY: default compile check clean

# How to convert Bison .y files into .cc files
%.cc: %.y
	bison -v -o $@ $<

# How to convert Flex .l files into .cc files
%.cc: %.l
	flex -o $@ $<

all: compile

compile: apyc

apyc: $(OBJS)
	$(CXX) -g -o $@ $(OBJS)

check: compile
	@echo Testing correct programs...
	rm -f errflag
	-$(GOODTEST) --compiler=$(APYC) tests/correct/*.py || touch errflag
	@echo
	@echo Testing erroneous programs...
	-$(BADTEST) --compiler=$(APYC) tests/error/*.py || touch errflag
	test ! -f errflag

clean:
	rm -f *~ *.o *.pyc *.ast *.tab.c *.output apyc errflag OUTPUT*
	rm -f tests/*/*.ast
	rm -f tests/*~ tests/*/*~
	rm -f $(GENERATED_SRCS)

really-clean: clean
	rm -f tests/correct/*.std

std:
	cd tests/correct; \
	for f in *.py; do \
		std=`basename $$f .py`.std; \
		echo python $$f ">" $$std; \
		python $$f > $$std; \
	done

# Dependencies



parser.cc: parser.y

lexer.cc: lexer.l

ast-parser.cc: ast-parser.y

ast-lexer.cc: ast-lexer.l

parser.o: parser.cc lexer.cc apyc.h
	$(CXX) -c $(CXXFLAGS) -Wno-sign-compare -o $@ $<

ast-parser.o: ast-parser.cc ast-lexer.cc apyc.h
	$(CXX) -c $(CXXFLAGS) -Wno-sign-compare -o $@ $<



nodes.o: nodes.cc ast.h apyc.h

ast.o: ast.cc ast.h apyc.h

ast-lexer.o: ast-lexer.cc

apyc.o: apyc.cc apyc.h

decls.o: decls.cc apyc.h

types.o: types.cc apyc.h

environment.o: environment.cc apyc.h

semantic.o: semantic.cc apyc.h